package com.finance.controller;

import com.finance.domain.Category;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CategoryController extends BaseController {


    /**
     * 跳转category-mgr.html页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/adminCategory")
    public String adminCategory(Model model) {
        List<Category> categories = categoryService.selectAllCategory(String.valueOf(session.getAttribute("USER_ID")));
        model.addAttribute("categories", categories);
        return "category-mgr";
    }

    @RequestMapping("/updateCategory")
    @ResponseBody
    public Map<String, Object> updateCategory(Category category) {
        Map<String, Object> map = new HashMap<>();
        int a = categoryService.updateCategory(category);
        if (a == 1) {
            map.put("data", "更新银行银行成功");
        } else {
            map.put("data", "更新银行银行失败");
        }
        return map;
    }

    @RequestMapping("/deleteCategory")
    @ResponseBody
    public Map<String, Object> deleteCategory(int cgid) {
        Map<String, Object> map = new HashMap<>();
        int a = categoryService.deleteCategory(cgid);
        if (a == 1) {
            map.put("data", "删除银行银行成功");
        } else {
            map.put("data", "删除银行银行失败");
        }
        return map;
    }

    @RequestMapping("/insertCategory")
    @ResponseBody
    public Map<String, Object> insertCategory(Category category) {
        Map<String, Object> map = new HashMap<>();
        category.setCreateBy(String.valueOf(session.getAttribute("USER_ID")));
        int a = categoryService.insertCategory(category);
        if (a == 1) {
            map.put("data", "增加银行银行成功");
        } else {
            map.put("data", "增加银行银行失败");
        }
        return map;
    }


}
